package com.thug.entities;

import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.AnimatedSprite;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.thug.engine.BaseGameEntity;
import com.thug.engine.Utility;

public class Player extends BaseGameEntity {

	public enum BodyType {
		BT_BODY, BT_UNKNOWN
	}

	public Body mBody;
	private AnimatedSprite mSprite;


	private Vector2 mDirection;
	private float mSpeed;
	private boolean mMoving;

	long[] mFrameDuration = new long[] { 100, 100, 100 };
	int[] mFrames = new int[] { 9, 10, 11 };


	public Player(AnimatedSprite s, Body b) {
		mEntityType = EntityType.ET_PLAYER;
		mBody = b;
		mSprite = s;
		mSpeed = 1;
		mDirection = new Vector2();
	}

	@Override
	public void init() {
	}

	@Override
	public void attach(Scene scene) {
		scene.attachChild(mSprite);

	}

	@Override
	public void detach(Scene scene) {
		scene.detachChild(mSprite);

	}

	@Override
	public void update(float elapsedTime) {

		if (isActive()) {

			Utility.updateSprite(mSprite, mBody, true, false);
			animateSprite();

			// update the position by controller
			mBody.setLinearVelocity(mDirection.mul(mSpeed));
			/*
			 * if(mMoving) { float ang = (float)Math.atan2(mDirection.y,
			 * mDirection.x); mBody.setTransform(mBody.getPosition(), ang); }
			 */
		}

	}

	@Override
	public void beginContact(Body bodyA, Body bodyB, Vector2 normal,
			Vector2[] points) {
		//mBotAI.beginContact(bodyA, bodyB, normal, points, this);

	}

	@Override
	public void endContact(Body bodyA, Body bodyB, Vector2 normal,
			Vector2[] points) {
		//mBotAI.endContact(bodyA, bodyB, normal, points, this);
	}

	@Override
	public void dispose() {
		//mBotAI.playerDestroyed(this);
		mSprite.detachSelf();
		mBody.getWorld().destroyBody(mBody);
		mSprite = null;
		mBody = null;
	}

	public void move(float valueX, float valueY) {
		mDirection.x = valueX;
		mDirection.y = valueY;
		float x = Math.abs(valueX);
		float y = Math.abs(valueY);
		mMoving = false;
		if ((x > 0.1f) || (y > 0.1f))
			mMoving = true;
	}

	public void setSpeed(float mSpeed) {
		this.mSpeed = mSpeed;
	}

	public void setPosition(float x, float y) {
		mSprite.setPosition(x, y);
	}

	public AnimatedSprite getSprite() {
		return mSprite;
	}

	public Body getBody() {
		return mBody;
	}

	public Vector2 getPostion() {
		return mBody.getPosition();
	}

	public Vector2 getDirection() {
		float angle = mBody.getAngle();
		float xCord = (float) Math.cos(angle);
		float yCord = (float) Math.sin(angle);

		Vector2 dir = new Vector2(xCord, yCord);
		return dir;
	}

	public BodyType containsBody(final Body body) {
		if (body == mBody)
			return BodyType.BT_BODY;
		else
			return BodyType.BT_UNKNOWN;
	}

	@Override
	public float getRotation() {
		return mSprite.getRotation();
	}

	@Override
	public float getX() {
		//return mSprite.getX();
		float x = mSprite.getX() + mSprite.getBaseWidth() * 0.5f;
		return x;
	}

	@Override
	public float getY() {
		//return mSprite.getY();
		float y = mSprite.getY() + mSprite.getBaseHeight() * 0.5f;
		return y;
	}

	private void animateSprite() {
		if (mMoving) {

			if (!mSprite.isAnimationRunning()) {

				float ang = (float) Math.atan2(mDirection.y, mDirection.x);
				ang = (float) Math.toDegrees(ang);
				if (ang < 0) {
					ang = ang + 360;
				}

				if (ang >= 315 || ang < 45) {
					setFrames(6, 7, 8); // right
				}

				else if (ang >= 45 && ang < 135) {
					setFrames(0, 1, 2); // down
				}

				else if (ang >= 135 && ang < 225) {
					setFrames(3, 4, 5); // left
				}

				else if (ang >= 225 && ang < 315) {
					setFrames(9, 10, 11); // up
				}

				mSprite.animate(mFrameDuration, mFrames, 0);
			}

		} else {
			mSprite.stopAnimation();
		}
		// }
	}

	private void setFrames(int a, int b, int c) {
		mFrames[0] = a;
		mFrames[1] = b;
		mFrames[2] = c;
	}

}